package com.common.www.utils;

import com.common.www.config.WebConfig;

import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class GlobalSessionUtils {
    public static HttpSession getSession() {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        //从获取RequestAttributes中获取HttpServletRequest的信息
        HttpServletRequest request = (HttpServletRequest) requestAttributes.resolveReference(RequestAttributes.REFERENCE_REQUEST);
        return request.getSession();
    }

    /**
     * 得到当前登录的用户
     */
    public static String getToken() {
        String token = null;
        if (getSession() != null) {
            Object o = getSession().getAttribute(WebConfig.ACCOUNT_KEY);
            token = o == null ? "" : (o + "");
        }
        return token;
    }
}
